package com.kdg.quene.arrayblockquene;

import java.util.concurrent.ArrayBlockingQueue;

/**
 * Created by kong on 2018/4/20.
 */
public class QueneTestOne {
    private int queneSize = 10;
    private ArrayBlockingQueue<Integer> quene = new ArrayBlockingQueue<Integer>(queneSize);

    public static void main(String[] args) {

        QueneTestOne queneTestOne = new QueneTestOne();
        Consumer consumer = queneTestOne.new Consumer();
        Producer producer = queneTestOne.new Producer();

        consumer.start();
        producer.start();

    }

    class Consumer extends Thread {
        @Override
        public void run() {
            consume();
        }
        private void consume() {
            while (true) {
                try {
                    quene.take();
                    System.out.println("从队列取走一个元素，队列剩余" + quene.size() + "个元素");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    class Producer extends Thread {
        @Override
        public void run() {
            producer();
        }

        private void producer() {
            while (true) {
                try {
                    quene.put(1);
                    System.out.println("往队列增加一个元素，队列大小为" + quene.size());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }


}
